from django.shortcuts import render, redirect
import datetime
import random
from django.core.paginator import Paginator
from .models import comment


# Create your views here.


def comments(request):

    if request.method == 'GET':
        com = comment.objects.order_by('-pub_date')
        paginator = Paginator(com, 5)
        page = request.GET.get('page')
        page_obj = paginator.get_page(page)
        id = "portrait/{}.jpg".format(random.randint(1,13))
        return render(request, 'comment.html', {'page_obj': page_obj, 'paginator': paginator, 'is_paginated': True})

    elif request.method == 'POST':
        username = request.POST['name']
        userid = request.POST['id']
        comment_s = request.POST['comment']
        title = request.POST['title']
        picture = request.FILES['picture']

        comment_ = comment()
        comment_.userid = userid
        comment_.username = username
        comment_.title = title
        comment_.picture = picture
        comment_.comments = comment_s

        comment_.pub_date = datetime.datetime.now()

        comment_.save()
        return redirect('comment')
